package book;
import static book.ConnectDB.cn;
import java.sql.*;

public class ConnectDB {
    public static Connection cn = null;
    private String portNumber;
    private String userName;
    private String password;
    private String database;
    private String url;
    private String server;
    
    public ConnectDB(){
        portNumber = "1433";
        userName ="sa";
        password= "123";
        database = "BookTHQ";
        server = "localhost";
        url = "jdbc:sqlserver://"+server+":"+portNumber+";database="+database;
    }

    public ConnectDB(String port , String user , String pass , String database ,String server){
         this.portNumber = port;
         this.userName = user;
         this.password = pass;
         this.database = database;
         this.server = server;
         this.url = "jdbc:sqlserver://"+this.server+":"+this.portNumber+";database="+this.database;
        
    }
    
    public boolean openConnect(){
        if(cn ==null){
            try{
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
             cn = DriverManager.getConnection(url,userName , password);
             if(cn!=null)
                 return true;            
            }catch(Exception ex){
                ex.printStackTrace();
                return false;
            }
        }
        return false;
    }
    
    public boolean closeConnect(){
        if(cn!=null){
            try{
               cn.close(); 
               cn=null;
               return true;
            }catch(Exception ex){
                ex.printStackTrace();
                return false; 
            }
        }
        return false;
    }
}
